<?php
namespace services;
class OpensslService{
    const keys = 'z3/z5dj3IrWsmNIRRdndkuJ6lbpzTEKCVXWHx0Nv3SE=';//echo base64_encode(openssl_random_pseudo_bytes(32));
    const iv = 'AU/3AH/xZ6zwI0hJ90YEXw==';//echo base64_encode(openssl_random_pseudo_bytes(16));
    const rsa_public_key='-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhjJLMNlDrVJIcM40WzK3UXtMN
KygRaaQyjkGt1/ysiDLaJ6AteT0Ve8zoVakYrJn1BURSz/m04ZRLI+JZMskMC3Mx
7mU4yxGn/Z1suskXaCGqcnjxTlO4n7qqx/23RkeAUCKmS4xFGfiAWCrTVucCZuJ/
NI/v/2QNEyrXUNlq2wIDAQAB
-----END PUBLIC KEY-----';
    const rsa_private_key='-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDhjJLMNlDrVJIcM40WzK3UXtMNKygRaaQyjkGt1/ysiDLaJ6At
eT0Ve8zoVakYrJn1BURSz/m04ZRLI+JZMskMC3Mx7mU4yxGn/Z1suskXaCGqcnjx
TlO4n7qqx/23RkeAUCKmS4xFGfiAWCrTVucCZuJ/NI/v/2QNEyrXUNlq2wIDAQAB
AoGAOyA6q0Xh2I8G7lWlOsDVIMLq2JzjmAz1ejtIhGYGWsQpcH7KwqDxnlwhzxYF
ie5VPaHv0zWp4/zufvQdNfzWLtAjjngASRF/RPB+YW/keJnWiivmxhorqrBsJmFz
tolf/iIcRTsWAOfJMRbbLaaNL+YmIitB05BWICFd3Zs5wQECQQDzJYWCg8E8qPY4
S8ok66Z90rOriXCqrLniGBJmpW7FmfhdBrNY3v83Qf8+hbD4U2pHEqva8OqWBXvt
1w3UGai7AkEA7XjoRUaZccs+Uix5EShbL1fJBjibYzGfImIuUg4Tf5E59L4Fyt9m
dmF+5OQwHLsxyzQD92S+7V4VS74EXCe0YQJAA/dPtzJI+6FR6tcpo/KIE7NVJO4n
/VC1838rBaubI/P//hx23nM8vH0CGdX6bgKGUER4syiM2weZ3uZMHnKQswJAPM8v
Oz4e4OLYrJYy0k/xOahTE8K/F6GHTHizv9NW7t2zZ5aZSR/3zuycX+SG5XSSBaTm
Nx2XPpVPArw/xZFtQQJBAOHcFpXroY3JjTQJoigTM3B/QT+J2tfS0xNo6PT7jeKX
7r+uYC8LKkTV+5zZBMB7iclGHHKjW67XwOzledpJ2Sw=
-----END RSA PRIVATE KEY-----';
    public function __construct() {
    }
    public function aes_encrypt($data){
        $encrypted = openssl_encrypt($data, 'aes-256-cbc', base64_decode(self::keys), OPENSSL_RAW_DATA, base64_decode(self::iv));
        return $this->base64url_encode($encrypted);
    }
    public function aes_decrypt($endata){
        $endata = $this->base64url_decode($endata);
        $decrypted = openssl_decrypt($endata, 'aes-256-cbc', base64_decode(self::keys), OPENSSL_RAW_DATA, base64_decode(self::iv));
        return $decrypted;
    }
    public function rsa_encrypt($data){
        openssl_public_encrypt($data, $encrypted,rsa_public_key);
        return $this->base64url_encode($encrypted);
    }
    public function rsa_decrypt($endata){
        $endata = $this->base64url_decode($endata);
        openssl_private_decrypt($encrypted, $decrypted, rsa_private_key);
        return $decrypted;
    }
    public function base64url_encode($s) {
        return str_replace(array('/', '=', '+'), array( '_', '~', ':'), base64_encode($s));
    }

    public function base64url_decode($s) {
        return base64_decode(str_replace(array('_', '~', ':'), array('/', '=', '+'), $s));
    }

}
